package com.gitee.linmt.entity;

import com.gitee.linmt.util.MessageUtil;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Objects;

/**
 * 缓存 code 与 message.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
public class ResultMessage<T> {
    
    private String unknownCodeMsgFormat = "未知消息，Code：{}";
    
    private CurdCode create;
    
    private CurdCode update;
    
    private CurdCode retrieve;
    
    private CurdCode delete;
    
    private Map<String, String> success;
    
    private Map<String, String> warning;
    
    private Map<String, String> failure;
    
    private Map<String, String> exception;
    
    /**
     * 根据结果获取信息.
     *
     * @param result 返回结果
     * @return 信息
     */
    public String getMessage(@NonNull final Result<T> result) {
        final String code = result.getCode();
        final ResultType resultType = result.getResult();
        final Object[] msgParam = result.getMsgParam();
        String message = null;
        if (code != null || resultType != null) {
            switch (resultType) {
                case SUCCESS:
                    if (!CollectionUtils.isEmpty(getSuccess())) {
                        // 获取成功消息
                        if (Objects.nonNull(result.getCurdType())) {
                            String successCurdCode = null;
                            switch (result.getCurdType()) {
                                case CREATE_SUCCESS:
                                    successCurdCode = create.getSuccess();
                                    break;
                                case UPDATE_SUCCESS:
                                    successCurdCode = update.getSuccess();
                                    break;
                                case RETRIEVE_SUCCESS:
                                    successCurdCode = retrieve.getSuccess();
                                    break;
                                case DELETE_SUCCESS:
                                    successCurdCode = delete.getSuccess();
                                    break;
                                default:
                                    break;
                            }
                            message = getSuccess().get(successCurdCode);
                        }
                        if (message == null) {
                            message = getSuccess().get(code);
                        }
                    }
                    break;
                case WARNING:
                    // 获取警告消息
                    if (!CollectionUtils.isEmpty(getWarning())) {
                        message = getWarning().get(code);
                    }
                    break;
                case FAILURE:
                    if (!CollectionUtils.isEmpty(getFailure())) {
                        if (Objects.nonNull(result.getCurdType())) {
                            String failureCurdCode = null;
                            switch (result.getCurdType()) {
                                case CREATE_FAILURE:
                                    failureCurdCode = create.getFailure();
                                    break;
                                case UPDATE_FAILURE:
                                    failureCurdCode = update.getFailure();
                                    break;
                                case RETRIEVE_FAILURE:
                                    failureCurdCode = retrieve.getFailure();
                                    break;
                                case DELETE_FAILURE:
                                    failureCurdCode = delete.getFailure();
                                    break;
                                default:
                                    break;
                            }
                            message = getFailure().get(failureCurdCode);
                        }
                        // 获取出错消息
                        if (message == null) {
                            message = getFailure().get(code);
                        }
                    }
                    break;
                case EXCEPTION:
                    // 获取异常消息
                    if (!CollectionUtils.isEmpty(getException())) {
                        message = getException().get(code);
                    }
                    break;
                default:
                    break;
            }
        }
        if (msgParam != null && msgParam.length != 0 && message != null) {
            message = MessageUtil.arrayFormat(message, msgParam);
        }
        return message;
    }
    
    public String getMessageByResult(@NonNull final Result<T> result) {
        String message = null;
        final String code = result.getCode();
        if (code != null || result.getCurdType() != null) {
            message = getMessage(result);
            if (message == null && StringUtils.hasText(getUnknownCodeMsgFormat())) {
                message = MessageUtil.format(getUnknownCodeMsgFormat(), result.getCode());
            }
        }
        return message;
    }
    
    public String getUnknownCodeMsgFormat() {
        return this.unknownCodeMsgFormat;
    }
    
    public void setUnknownCodeMsgFormat(final String unknownCodeMsgFormat) {
        this.unknownCodeMsgFormat = unknownCodeMsgFormat;
    }
    
    public CurdCode getCreate() {
        return this.create;
    }
    
    public void setCreate(final CurdCode create) {
        this.create = create;
    }
    
    public CurdCode getUpdate() {
        return this.update;
    }
    
    public void setUpdate(final CurdCode update) {
        this.update = update;
    }
    
    public CurdCode getRetrieve() {
        return this.retrieve;
    }
    
    public void setRetrieve(final CurdCode retrieve) {
        this.retrieve = retrieve;
    }
    
    public CurdCode getDelete() {
        return this.delete;
    }
    
    public void setDelete(final CurdCode delete) {
        this.delete = delete;
    }
    
    public Map<String, String> getSuccess() {
        return this.success;
    }
    
    public void setSuccess(final Map<String, String> success) {
        this.success = success;
    }
    
    public Map<String, String> getWarning() {
        return this.warning;
    }
    
    public void setWarning(final Map<String, String> warning) {
        this.warning = warning;
    }
    
    public Map<String, String> getFailure() {
        return this.failure;
    }
    
    public void setFailure(final Map<String, String> failure) {
        this.failure = failure;
    }
    
    public Map<String, String> getException() {
        return this.exception;
    }
    
    public void setException(final Map<String, String> exception) {
        this.exception = exception;
    }
}
